<template>
  <div>
    <el-upload
      list-type="picture-card"
      action="#"
      :on-preview="preview"
      :limit="1"
      :file-list="fileList"
      :class="{ disabled: fileComputed }"
      :on-remove="handleRemove"
      :on-change="changeFile"
      :before-upload="beforeUpload"
      :http-request="upload"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <el-progress
      v-if="showProgress"
      :percentage="progress"
      style="width:180px"
    />
    <!-- 预览图片 -->
    <el-dialog :visible.sync="showDialog" title="图片预览">
      <img :src="imgUrl" alt="" style="width:100%">
    </el-dialog>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKIDL3Xu64JvhJfli2pzVowsJYgiGb3LhwSo',
  SecretKey: 'JvJYgXCiz1PdeP9crpdgdC8MM7cx4w86'
})
export default {
  props:{

  },
  data() {
    return {
      showDialog: false,
      imgUrl: '',
      currentFildeUid: null,
      fileList: [{ url: this.staffPhoto }],
      progress: 0,
      showProgress: false
    }
  },
  computed: {
    fileComputed() {
      return this.fileList.length === 1
    }
  },
  methods: {
    preview(file) {
      console.log(file)
      this.imgUrl = file.url
      this.showDialog = true
    },
    handleRemove(file, fileList) {
      this.fileList = this.fileList.filter(item => item.uid !== file.uid)
    },
    changeFile(file, fileList) {
      this.fileList = fileList.map(item => item)
    },
    beforeUpload(file) {
      const fileType = ['image/jpeg', 'image/gif', 'image/bmp', 'image/png']
      // 检查类型
      if (!fileType.some(item => item === file.type)) {
        this.$message.error('上传图片类型只能是JPG、GIF、BMP、PNG 格式!')
        return false
      }
      // 检查上传图片的大小
      const fileMaxSize = 5 * 1024 * 1024
      if (file.size > fileMaxSize) {
        this.$message.error('可上传图片最大为5MB')
        return false
      }
      this.currentFildeUid = file.uid
      this.showProgress = true
      // 一定要return true不然不会往下执行
      return true
    },
    upload(params) {
      if (params.file) {
        cos.putObject(
          {
            Bucket: 'yihua-1305625910',
            Region: 'ap-guangzhou',
            Key: params.file.name,
            Body: params.file,
            StorageClass: 'STANDARD',
            onProgress: progressData => {
              this.progress = progressData.percent * 100
            }
          },
          (err, data) => {
            // console.log(1)
            // console.log(err || data)
            if (!err && data.statusCode === 200) {
              this.fileList = this.fileList.map(item => {
                if (item.uid === this.currentFildeUid) {
                  return { url: 'http://' + data.Location, upload: true }
                }
                return item
              })
              // 关闭进度条，重置百分比
              this.showProgress = false
              this.progress = 0
              this.$emit('updatestaffPhoto', this.fileList)
            }
          }
        )
      }
    }
  }
}
</script>

<style lang="scss">
.disabled .el-upload--picture-card {
  display: none;
}
</style>
